package org.createModel.singleton.demo7;

import java.io.*;

public class Client {
    public static void main(String[] args) {
        try {
            Singleton instance = Singleton.getInstance();
            writeObjectToFile(instance);
            Singleton singleton = readObjectToFile();
            System.out.println(instance == singleton);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Singleton readObjectToFile() throws Exception{
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:\\IdeaProjects\\designModle\\src\\main\\resources\\singleton"));
        Singleton singleton = (Singleton)objectInputStream.readObject();
        objectInputStream.close();
        return singleton;
    }

    // 写对象到文件
    public static void writeObjectToFile (Singleton instance) throws Exception {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\IdeaProjects\\designModle\\src\\main\\resources\\singleton"));
        objectOutputStream.writeObject(instance);
        objectOutputStream.close();
    }
}